/*
* @(#)ListDmsViewPublicFolderAccessAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/

package com.dcivision.dms.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.user.bean.UserGroupPermission;
import com.dcivision.user.bean.UserRecordPermission;
import com.dcivision.user.bean.UserRolePermission;
import com.dcivision.user.dao.UserGroupPermissionDAObject;
import com.dcivision.user.dao.UserRecordPermissionDAObject;
import com.dcivision.user.dao.UserRolePermissionDAObject;

/**
 * <p>Class Name:       ListDmsViewPublicFolderAccessAction.java    </p>
 * <p>Description:      The list action class for ListDmsViewPublicFolderAccess.jsp</p>
    @author          Beyond Qu
    @company         DCIVision Limited
    @creation date   01/11/2004
    @version         $Revision: 1.4 $
 */
public class ListDmsViewPublicFolderAccessAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.4 $";


  /**
   *  Constructor - Creates a new instance of ListDmsPublicDocumentAction and define the default listName.
   */
  public ListDmsViewPublicFolderAccessAction() {
    super();
    this.setListName("ViewPublicFolderAccessList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.dms.dao.DmsDocumentDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_EMPTY_FILE);
  }

  /**
   * getListData
   *
   * Override the parent's function. Purpose in create the default personal folder when non-exists,
   * and load the dmsDocument list.
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    List resultList = new ArrayList();
    List accessDocIDList = new ArrayList();
    String accessDocIDStr = "";
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    
    String parentIDStr = request.getParameter("parentID");
    String rootIDStr = request.getParameter("rootID");    
    
    Integer parentID = TextUtility.parseIntegerObj(parentIDStr);
    Integer rootID = TextUtility.parseIntegerObj(rootIDStr);

    try {
      UserGroupPermissionDAObject   userGroupPermissionDAObject  = new  UserGroupPermissionDAObject(sessionContainer, conn);
      UserRecordPermissionDAObject  userRecordPermissionDAObject = new UserRecordPermissionDAObject(sessionContainer, conn);
      UserRolePermissionDAObject    userRolePermissionDAObject   = new UserRolePermissionDAObject(sessionContainer, conn);
      DocumentRetrievalManager documentRetrievalManager =  new DocumentRetrievalManager(sessionContainer, conn);
      //get All access setting folder or document id string just like : "id1,id2,id3"
      accessDocIDList = userGroupPermissionDAObject.getListByObjectType(GlobalConstant.OBJECT_TYPE_DOCUMENT);      
      for(int i = 0 ; i< accessDocIDList.size() ;i++){
        UserGroupPermission userGroupPermission = (UserGroupPermission)accessDocIDList.get(i);
        if(accessDocIDStr.length() != 0){
            accessDocIDStr += ",";
        }
        accessDocIDStr += userGroupPermission.getObjectID() ;
      }
      
      accessDocIDList = userRecordPermissionDAObject.getListByObjectType(GlobalConstant.OBJECT_TYPE_DOCUMENT);      
      for(int i = 0 ; i< accessDocIDList.size() ;i++){
        UserRecordPermission userRecordPermission = (UserRecordPermission)accessDocIDList.get(i);
        if(accessDocIDStr.length() != 0){
            accessDocIDStr += ",";
        }
        accessDocIDStr += userRecordPermission.getObjectID() ;
      }
      
      accessDocIDList = userRolePermissionDAObject.getListByObjectType(GlobalConstant.OBJECT_TYPE_DOCUMENT);      
      for(int i = 0 ; i< accessDocIDList.size() ;i++){
        UserRolePermission userRolePermission = (UserRolePermission)accessDocIDList.get(i);
        if(accessDocIDStr.length() != 0){
            accessDocIDStr += ",";
        }
        accessDocIDStr += userRolePermission.getObjectID() ;
      }
      
      
      resultList = documentRetrievalManager.getAccessFolderByParentIDRootID(parentID,rootID,accessDocIDStr);
      request.setAttribute(this.m_sListName, resultList);
      sessionContainer=null;
    } catch (Exception e) {
      log.error(e, e);
      this.rollback(request);
      throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
    }finally{
      conn = null;
    }
  }

}

